// movelist.js

// movelist class
chess_spider.MoveList = function( movelist_id, head, adjust ) {

	// params
	this.movelist_id = movelist_id;
	this.head = (head===false) ? false : head;
	this.adjust = (adjust===false) ? false : true;
	this.game = null;

	// init dom
	this.dom_div = document.getElementById(this.movelist_id);
	if( !this.dom_div ) throw( 'unable to open movelist "'+this.movelist_id+'"' );
	this.dom_div.className = 'cs__movelist_container';

	// set to movelists
	chess_spider.params.movelists[this.movelist_id] = this;
}

chess_spider.MoveList.initAll = function() {

	// make objects in movelists into MoveLists
	for( var movelist_id in chess_spider.params.movelists ) {
		if( typeof movelist_id != 'string' || typeof chess_spider.params.movelists[movelist_id] != 'object' ) continue;
		new chess_spider.MoveList( movelist_id, chess_spider.params.movelists[movelist_id].head );
	}
}

// all movelists instantiated in document
chess_spider.MoveList.movelists = function() {
	var result_arr = new Array();
	var dom_arr = document.getElementsByClassName('cs__movelist_container');
	for( var i=0; i<dom_arr.length; i++ )
	if( dom_arr[i].id && chess_spider.params.movelists[dom_arr[i].id] ) {
		result_arr.push( chess_spider.params.movelists[dom_arr[i].id] );
	}
	return result_arr;
}

// helpers
chess_spider.MoveList.display = function( gamelist_id, game ) { chess_spider.gamelists[gamelist_id].display( game ); }

// set game, displays its moves, attach self to document
chess_spider.MoveList.prototype.display = function( game ) {

	// save home
	if( chess_spider.state.home && chess_spider.params.home_id ) {
		chess_spider.state.home_pnt = document.getElementById(chess_spider.params.home_id).parentNode;
		chess_spider.state.home_dom = document.getElementById(chess_spider.params.home_id);
	}

	// we're not home anymore
	chess_spider.state.home = false;

	// if same game then we're done
	// nope: could have been home
	//=if( this.dom_div && this.game == game && game.dom_movelist ) return;

	// init our dom_div
	if( !this.dom_div ) {
		this.dom_div = document.getElementById(this.movelist_id);
		this.dom_div.className = 'cs__movelist_container';
	}

	// set game
	this.game = game;

	// if game has already instantiated it's dom_movelist, simply put it there
	if( !this.game.dom_movelist ) {

		// create game's dom
		this.game.dom_movelist = document.createElement('div');
		this.game.dom_movelist.class_name = 'cs__movelist_subcontainer';
		this.game.dom_movelist.id = 'cs__movelist_'+this.game.idx;

		// head
		this.writeHead();

		// load game
		//=var frag = document.createDocumentFragment();

		// bootstrap root move
		//=frag.appendChild( E.div( '.cs__l0', E.span( '.cs__move', E.a( 'move', null, {'a.move_id':this.game.root_mv.id,'text':this.game.root_mv.move} ) ) ) );
		this.game.dom_movelist.appendChild(
			E.div( '.cs__l0', E.span( '.cs__move', E.a( 'move', null, {'a.move_id':this.game.root_mv.id,'text':this.game.root_mv.move} ) ) ) );

		// add cdr moves
		//=for( var move=this.game.root_mv.nxt; move; move=move.nxt ) move.domAdd( frag );
		for( var move=this.game.root_mv.nxt; move; move=move.nxt ) move.domAdd();
		//=this.game.dom_movelist.appendChild( frag );
	}

	// remove any subcontainers in movelist and show for game
	if( this.dom_div.firstChild ) this.dom_div.removeChild( this.dom_div.firstChild );
	this.dom_div.appendChild( this.game.dom_movelist );

	// reposition
	this.reposition();
}

// write head (title)
chess_spider.MoveList.prototype.writeHead = function( new_head ) {

	var head = ( new_head ) ? new_head : this.head;

	// make sure head in place
	if( head === false && this.game.dom_movelist.getElementsByClassName('cs__movelist_head').length ) {
		this.game.dom_movelist.removeChild( this.game.dom_movelist.getElementsByClassName('cs__movelist_head')[0] );
	}
	else if( head !== false && !this.game.dom_movelist.getElementsByClassName('cs__movelist_head').length ) {
		var head = document.createElement('div');
		head.className = 'cs__movelist_head';
		this.game.dom_movelist.insertBefore( head, this.game.dom_movelist.firstChild );
	}

	// no head at all
	if( head === false ) return;

	// head
	var buf = ( typeof head == 'string' ) ? head : ((this.game) ? this.game.fullTitle() : '(filled in when game initialized');
	this.game.dom_movelist.getElementsByClassName('cs__movelist_head')[0].innerHTML = buf;
}

// reposition in parent
chess_spider.MoveList.prototype.reposition = function() {

	// not allowed
	if( !this.adjust ) return;

	// parent to set inside of 
	var pnt = this.dom_div.parentNode;

	// must have parent and guts
	if( !this.dom_div.innerHTML || !pnt ) return;

	// expecting to go to bottom
	this.dom_div.style.height = pnt.clientHeight - (this.dom_div.offsetTop + 10);
}

// redisplay moves
chess_spider.MoveList.prototype.redisplay = function() {
	var href_arr = this.dom_div.getElementsByTagName('A');
	for( var i=0; i<href_arr.length; i++ ) {
		if( !href_arr[i].firstChild ) continue;
		if( !href_arr[i].getAttribute('move_id') ) continue;
		//-console.log( href_arr[i].firstChild );
		href_arr[i].firstChild.nodeValue = chess_spider.state.moves[href_arr[i].getAttribute('move_id')].getMove();
	}
}

